gapstart= T-nlags;                                               % Show Full sample of regression
startdate = size(varnames,1)-gapstart;                         % Set starting date 
timestr = varnames(startdate:end,1);                            % Retrieve dates
spacing = size(timestr,1);                                      % Set end date on axes



histirf_med_boom=median(histirf(:,:,:,:) -histirf(:,datelow,:,:) ,4);
histirf_up_boom=prctile(histirf(:,:,:,:) -histirf(:,datelow,:,:),0.84*100,4);
histirf_down_boom=prctile(histirf(:,:,:,:) -histirf(:,datelow,:,:),0.16*100,4);

histirf_med_bust=median(histirf(:,datehigh,:,:) -histirf(:,:,:,:) ,4);
histirf_up_bust=prctile(histirf(:,datehigh,:,:) -histirf(:,:,:,:),0.84*100,4);
histirf_down_bust=prctile(histirf(:,datehigh,:,:) -histirf(:,:,:,:),0.16*100,4);





for ii=1:11; spacing = [spacing(1,1+end-ii)-16, spacing]; end


%% Comparison of contribtuions
f6=figure('Name','Figure 4: Decomposition of Real House Prices with Error Bands','Units','centimeters','Position',[5 1 26 32]);
  for shock=1:no_str
       subplot(no_str,1,shock)             
       hd1=plot(1:TT,  squeeze(histirf_med(n_decomp,:,shock))','--g'); hold on, ...
       hd4=plot(1:TT,  squeeze(mean(histirf(n_decomp,:,shock,:),4))','-b'); hold on, ...
       hd5=plot(1:TT,  squeeze(histirf_down(n_decomp,:,shock))',':r'); hold on, ...
       hd6=plot(1:TT,  squeeze(histirf_up(n_decomp,:,shock))',':r'); hold on, ...
       title(['Contribution of ' shockname2{shock}])
       set(gca, 'XTick', spacing, 'XTickLabel', timestr(spacing));   
%       xlim([1 gapstart])
       ylim([-20 40])
  end
lh=legend([hd1  hd4 hd5],{'Median','Mean','16th-84th perc.'},'Location','SouthEast','Orientation','Horizontal');      
set(lh,'Position', [0.5 0.02 0.05 0.05],'FontSize',10);
set(gca,'FontSize',10)
legend('boxoff') 
% axis tight  
set(gcf, 'Name','Figure 4: Decomposition of Real House Prices with Error Bands','NumberTitle','off','PaperPositionMode','auto') 
 % Graphs Name and Save
if savepic==1; grtitle=['perc'];   print(f6,'-dpng','-zbuffer',[outputfolder grtitle ]) ; end


%% SAVE CONTRIBUTION OVER TIME FOR MEAN AND PERCENTILES TO EXCEL
if savexls==1
clear ContribSum    
%Boom    
Contrib_legend=[shockname2, {'Residual','Deterministic','House Price'}];
ContribSum(1,:)=[squeeze(histirf_cent(n_decomp,datehigh,1:no_str)-histirf_cent(n_decomp,datelow,1:no_str))' (sum(histirf_cent(n_decomp,datehigh,no_str+1:n_var),3)-sum(histirf_cent(n_decomp,datelow,no_str+1:n_var),3))  (histempt_cent(n_decomp,datehigh,2)-histempt_cent(n_decomp,datelow,2)) (realdata(datehigh,1)-realdata(datelow,1))] /(realdata(datehigh,1)-realdata(datelow,1));
ContribSum(2,:)=[squeeze(histirf_med_boom(n_decomp,datehigh,1:no_str))' NaN  (histempt_cent(n_decomp,datehigh,2)-histempt_cent(n_decomp,datelow,2)) (realdata(datehigh,1)-realdata(datelow,1))] /(realdata(datehigh,1)-realdata(datelow,1));
ContribSum(3,:)=[squeeze(histirf_up_boom(n_decomp,datehigh,1:no_str))' NaN  (histempt_cent(n_decomp,datehigh,2)-histempt_cent(n_decomp,datelow,2)) (realdata(datehigh,1)-realdata(datelow,1))] /(realdata(datehigh,1)-realdata(datelow,1));
ContribSum(4,:)=[squeeze(histirf_down_boom(n_decomp,datehigh,1:no_str))' NaN  (histempt_cent(n_decomp,datehigh,2)-histempt_cent(n_decomp,datelow,2)) (realdata(datehigh,1)-realdata(datelow,1))] /(realdata(datehigh,1)-realdata(datelow,1));

%Bust  
ContribSum(5,:)=[squeeze(histirf_cent(n_decomp,datehigh,1:no_str)-histirf_cent(n_decomp,datebust,1:no_str))' (sum(histirf_cent(n_decomp,datehigh,no_str+1:n_var),3)-sum(histirf_cent(n_decomp,datebust,no_str+1:n_var),3))  (histempt_cent(n_decomp,datehigh,2)-histempt_cent(n_decomp,datebust,2)) (realdata(datehigh,1)-realdata(datebust,1))] /(realdata(datehigh,1)-realdata(datebust,1));
ContribSum(6,:)=[squeeze(histirf_med_bust(n_decomp,datebust,1:no_str))' NaN  (histempt_cent(n_decomp,datehigh,2)-histempt_cent(n_decomp,datebust,2)) (realdata(datehigh,1)-realdata(datebust,1))] /(realdata(datehigh,1)-realdata(datebust,1));
ContribSum(7,:)=[squeeze(histirf_up_bust(n_decomp,datebust,1:no_str))' NaN  (histempt_cent(n_decomp,datehigh,2)-histempt_cent(n_decomp,datebust,2)) (realdata(datehigh,1)-realdata(datebust,1))] /(realdata(datehigh,1)-realdata(datebust,1));
ContribSum(8,:)=[squeeze(histirf_down_bust(n_decomp,datebust,1:no_str))' NaN  (histempt_cent(n_decomp,datehigh,2)-histempt_cent(n_decomp,datebust,2)) (realdata(datehigh,1)-realdata(datebust,1))] /(realdata(datehigh,1)-realdata(datebust,1));



xlswrite([outputfolder 'BoomPriceContrib.xlsx'],[{'Date'}, {'Measure'},Contrib_legend; {'Boom'; ''; ''; ''; 'Bust';''; ''; ''}, {'Mean'; 'Median'; '84th'; '16th';'Mean'; 'Median'; '84th'; '16'} , num2cell(ContribSum)], ['Sum' 'perc'  ],'B2');



end


